<template>
  <div class="register-container">
    <van-nav-bar left-arrow title="注册个人信息" @click-left="this.$router.go(-1)" />
    <van-form @submit="onSubmit" ref="registerRef">
      <van-cell-group inset>
        <van-field
          v-model="userlist.username"
          name="账户名"
          label="账户名"
          placeholder="请输入账户名"
          autocomplete="off"
          :rules="rules.name"
        />
        <van-field
          v-model="userlist.name"
          name="昵称"
          label="昵称"
          placeholder="请输入昵称"
          autocomplete="off"
          :rules="rules.username"
        />
        <van-field
          v-model="userlist.phone"
          name="手机号"
          label="手机号"
          placeholder="请输入手机号"
          autocomplete="off"
          :rules="rules.phone"
        />
        <van-field
          v-model="userlist.email"
          name="邮箱"
          label="邮箱"
          placeholder="请输入邮箱"
          autocomplete="off"
          :rules="rules.email"
        />
        <van-field name="radio" label="性别">
          <template #input>
            <van-radio-group v-model="this.userlist.sex" direction="horizontal">
              <van-radio name="0">女</van-radio>
              <van-radio name="1">男</van-radio>
            </van-radio-group>
          </template>
        </van-field>
        <van-field
          v-model="userlist.password"
          type="password"
          name="密码"
          label="密码"
          placeholder="请输入密码"
          :rules="rules.password"
        />
        <van-field
          v-model="confirmPassword"
          type="password"
          name="密码"
          label="密码"
          placeholder="请再次输入密码"
          :rules="rules.confirmPassword"
        />
      </van-cell-group>
    </van-form>

    <van-button type="primary" @click="onSubmit">提交</van-button>
  </div>
</template>

<script>
import { showToast, showDialog } from 'vant'
import { Register } from '../../api/user.js'
export default {
  data() {
    return {
      userlist: {
        name: '',
        username: '',
        phone: '',
        email: '',
        sex: '',
        point: 0,
        password: ''
      },
      confirmPassword: '',
      // 校验规则
      rules: {
        name: [{ required: true, message: '请填写用户名', trigger: 'blur' }],
        username: [{ required: true, message: '请填写昵称', trigger: 'blur' }],
        phone: [
          { required: true, message: '请填写手机号', trigger: 'blur' },
          { pattern: /^1[3456789]\d{9}$/, message: '手机号码格式错误！', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请填写邮箱', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/,
            message: '邮箱格式错误！'
          }
        ],
        password: [
          { required: true, message: '请填写密码', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{8,16}$/, message: '密码格式错误，需包含字母、数字' }
        ],
        confirmPassword: [
          { required: true, message: '请填写密码', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{8,16}$/, message: '密码格式错误，需包含字母、数字' }
        ]
      }
    }
  },
  methods: {
    onSubmit() {
      if (this.userlist.password !== this.confirmPassword) {
        showToast('密码两次输入不一致！')
        this.confirmPassword = ''
        return
      }
      this.$refs.registerRef
        .validate()
        .then(async () => {
          const { data: res } = await Register(this.userlist)
          if (res === '注册成功') {
            showToast('注册成功！')
            setTimeout(() => {
              this.$router.go('/login')
            }, 3000)
          } else {
            showDialog({
              message: res
            }).then(() => {
              showToast('注册失败！')
            })
          }
        })
        .catch(() => {
          // 验证失败
          showToast('注册失败，注册信息有误。')
        })
    }
  }
}
</script>

<style lang="less" scoped>
.register-container {
  height: 100vh;

  .van-form {
    margin-top: 67px;

    .van-field {
      margin: 10px 0;
    }
  }
  .van-button {
    width: 80%;
    left: 50%;
    transform: translateX(-50%);
    margin-top: 30px;
    font-size: 16px;
  }
}
</style>
